package it.javaweb.actions.utenti;

import it.javaweb.beans.Utente;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.validation.ValidationUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class PasswordController extends SimpleFormController {

	public PasswordController() {
		setCommandClass(PasswordCommand.class);
	}
	
	@Override
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		PasswordCommand cmd = (PasswordCommand) super.formBackingObject(request);
		cmd.setLogin("Scrivi il tuo login");
		return cmd;
	}
	
	@Override
	protected Map referenceData(HttpServletRequest request) throws Exception {
		List<String> comuni = new ArrayList<String>();
		comuni.add("Firenze");
		comuni.add("Scandicci");
		comuni.add("Barberino");
		comuni.add("etc...");
		Map refdata = super.referenceData(request);
		if ( refdata==null ) refdata = new TreeMap();
		refdata.put("comuni", comuni);
		
		return refdata;
	}
	
	@Override
	protected ModelAndView onSubmit(Object command, BindException errors) throws Exception {
		PasswordCommand cmd = (PasswordCommand) command;
		
		//TODO recuperare mail del tizio
		Utente utente = null;
		
		// Se il login inserito NON corrisponde a nessun utente del sistema
		// si visualizza un errore
		if ( utente==null ) {
			//todo
		} else {
			//TODO inviare la mail
		}
		
		
		return new ModelAndView(getSuccessView());
	}
	
	
}
